<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
	<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
	<title>编辑商品</title>
	<link rel="stylesheet" href="./Public/Admin/css/bootstrap.min.css" />
	<link rel="stylesheet" href="./Public/Admin/css/common.css" />

	<!-- 需要载入的文件 -->
	<script src="./Public/Admin/js/jquery-1.8.2.min.js" type="text/javascript" charset="utf-8"></script>
    <script type="text/javascript" src="./Public/Admin/uploadify/jquery.uploadify.min.js"></script>
    <link rel="stylesheet" type="text/css" href="./Public/Admin/uploadify/uploadify.css">
    
    <!--载入百度编辑器-->
	<script type="text/javascript" charset="utf-8" src="./Public/Admin/ueditor1_4_3/ueditor.config.js"></script>
	<script type="text/javascript" charset="utf-8" src="./Public/Admin/ueditor1_4_3/ueditor.all.min.js"> </script>
	<script type="text/javascript" charset="utf-8" src="./Public/Admin/ueditor1_4_3/lang/zh-cn/zh-cn.js"></script>
	    
	<script type="text/javascript">
//*********商品异步添加**********
		$(function(){
			//对name="wsshop_category_cid"进行change事件处理
			$('select[name=wsshop_category_cid]').change(function(){
				//获取选中的所属分类的option的tid
				var tid = $('option:selected').attr('tid');//获取选择的类型ID
				$('input[name=wsshop_type_tid]').val(tid);//把选择的对应的类型ID添加到隐藏域
//				alert(tid);
				//进行ajax处理
				$.ajax({
					//传输数据到php方式
					type:"post",
					//传到哪
					url:"<?php echo U('Goods/getAttr')?>",
					//传输数据
					data:{tid : tid},
					//确定返回数据类型
					dataType:'json',
					//数据成功返回
					success:function(phpData){
//						console.log(phpData);
						var attr = '';//属性
						var spec = '';//规格
						//遍历phpData
						$.each(phpData, function(k,v) {
							//如果是属性
							if(v.class =="属性"){
								attr += '<tr class="info">';
								attr += '<td>'+v.taname+'</td>';
								attr += '<td>';
								attr += '<select name="attr['+v.taid+']" id="">';
								attr += '<option value="">请选择</option>';
									 $.each(v.tavalue, function(kk,vv) {    
										attr += '<option value="'+vv+'">'+vv+'</option>';
									 });
								attr += '</select>';
								attr += '</td></tr>';
							}else{//如果是规格
								spec += '<tr class="info">';
								spec += '<td>'+v.taname+'</td>';
								spec += '<td>';
								spec += '<select name="spec[value]['+v.taid+'][]" id="">';
								spec += '<option value="">请选择</option>';
									$.each(v.tavalue, function(kk,vv) {    
										spec += '<option value="'+vv+'">'+vv+'</option>';                                                          
									});
								spec += '</select></td>';
								spec += '<td>附加价格：<input type="text" name="spec[extraprice]['+v.taid+'][]" id=""/></td>';
								spec += '<td>';
								spec += '<a href="javascript:;" class="btn btn-primary addSpec">添加规格</a>';
								spec += '</td></tr>';
							}
						});
						//异步添加商品属性和规格
						$('#attr').html(attr);
						$('#spec').html(spec);
					}
				});
			})
			//点击添加规格
			$('.addSpec').live('click',function(){
				//找到当前的父级的tr
				var trObj = $(this).parents('tr');
				//克隆
				var tr = trObj.clone();
				tr.find('.addSpec').html('删除规格').removeClass('btn-primary addSpec').addClass('btn-info removeSpec');
				//然后插入到当前父级 的下面
				trObj.after(tr);
			})
			//点击删除表格后删除表格
			$('.removeSpec').live('click',function(){
				//删除表格的父级
				$(this).parents('tr').remove();
			})
			
		
//*********END商品异步添加*************		

//**********上传列表图**************
			$('#listImg').uploadify({
				    'formData'     : {//POST数据
				        '<?php echo session_name();?>' : '<?php echo session_id();?>',
				    },
				    'fileTypeExts' : '*.jpg;*.png;*.jpeg;*.gif',
				    'swf'      : '<?php echo __PUBLIC__?>/Admin/uploadify/uploadify.swf',
				    'uploader' : '<?php echo U('uploadList')?>',//指定服务器上传的方法
				    'buttonText':'选择文件',
				    'fileSizeLimit' : '2000KB',
				    'uploadLimit' : 1,//上传文件数
				    'width':65,
				    'height':25,
				    'successTimeout':10,//等待服务器响应时间
				    'removeTimeout' : 0.2,//成功显示时间
				    'onUploadSuccess' : function(file, data, response) {
				        //把php返回的数据转为json
				        data=$.parseJSON(data);
				        //获得图片地址
				        var imageUrl = data.url;
				        var li="<li style='list-style:none;positon:absolute;'>";
				        li += "<img src='"+imageUrl+"'style='height:100px;'/>";
				        li += "<input type='hidden' name='listview' value='"+data.path+"'/><a href='javascript:;' id='self-closeA' class='btn btn-sm btn-danger'>X</a>";
				        li += "</li>";
				        $("#uploadList ul").prepend(li);
				    }
				});
				//关闭商品列表图片
				var j = 1;
				$('#self-closeA').live('click',function(){
				   	//移除界面的图片
				    $(this).parent('li').remove();
				    j++;
				    $('#listImg').uploadify('settings','uploadLimit',j);
				    //获取删除图片的路径-删除图片
				    var delImg = $(this).siblings('input').val();
				   	$.ajax({
				   		type:"post",
				    	url:"<?php echo U('Goods/delImg')?>",
				    	data:{delImg : delImg},
				    	dataType:'json',
				   	});
				})
			//**********END上传列表图**************
			
			//*************商品图册******************
			$('#shopImg').uploadify({
				    'formData'     : {//POST数据
				        '<?php echo session_name();?>' : '<?php echo session_id();?>',
				    },
				    'fileTypeExts' : '*.jpg;*.png;*.jpeg;*.gif',
				    'swf'      : '<?php echo __PUBLIC__?>/Admin/uploadify/uploadify.swf',
				    'uploader' : '<?php echo U('uploadShopImg')?>',//指定服务器上传的方法
				    'buttonText':'选择文件',
				    'fileSizeLimit' : '2000KB',
				    'uploadLimit' : 1,//上传文件数
				    'width':65,
				    'height':25,
				    'successTimeout':10,//等待服务器响应时间
				    'removeTimeout' : 0.2,//成功显示时间
				    'onUploadSuccess' : function(file, data, response) {
				        //把php返回的数据转为json
				        data=$.parseJSON(data);
				        //获得图片地址
				        var imageUrl = data.url;
				        var li="<li style='list-style:none;positon:absolute;'>";
				        li += "<img src='"+imageUrl+"'style='height:100px;'/>";
				        li += "<input type='hidden' name='gdpic[]' value='"+data.path+"'/><a href='javascript:;' id='self-close' class='btn btn-sm btn-danger'>X</a>";
				        li += "</li>";
				        $("#uploadShopImg ul").prepend(li);
				    }
				});
				//关闭商品图册的图片
				var j = 1;
				$('#self-close').live('click',function(){
				   	//移除界面的图片
				    $(this).parent('li').remove();
				    j++;
				    $('#shopImg').uploadify('settings','uploadLimit',j);
				    //获取删除图片的路径-删除图片
				    var delShopImg = $(this).siblings('input').val();
				   	$.ajax({
				   		type:"post",
				    	url:"<?php echo U('Goods/delShopImg')?>",
				    	data:{delShopImg : delShopImg},
				    	dataType:'json',
				   	});
				})
			//**************END商品图册***************8
		})			
	</script>
	
</head>
<body>
	<div class="container-fluid">
	<div class="row-fluid">
	<div class="span12">
	<form action="" method='post' enctype="multipart/form-data">
		<fieldset>
			<legend style="text-align: center;">编辑商品</legend>
			<table class='table table-bordered table-hover'>
				<thead>
					<tr>
						<th colspan="2" class="btn btn-primary">基本信息</th>
					</tr>
				</thead>
				<tbody>
					<tr class="info">
						<td>所属分类</td>
						<td>
							<select name="wsshop_category_cid">
								<option value="0">-请选择-</option>
									<?php foreach ($cateData as $v){?>
									<option value="<?php echo $v['cid']?>" tid=<?php echo $v['wsshop_type_tid']?> <?php if($oldData['wsshop_category_cid'] == $v['cid']): ?>selected<?php endif ?> ><?php echo $v['_name']?></option>
									<?php }?>
							</select>
						</td>
					</tr>
					<tr class="info">
						<td>所属品牌</td>
						<td>
							<select name="wsshop_brand_bid">
								<option value="0">-请选择-</option>
								<?php foreach ($brandData as $v){?>
								<option value="<?php echo $v['bid']?>" <?php if($oldData['bid'] == $v['bid']): ?>selected<?php endif ?> ><?php echo $v['bname']?></option>
								<?php }?>
							</select>
						</td>
					</tr>
					<tr class="info">
						<td>商品名称</td>
						<td>
							<input type="text" name='gname' value="<?php echo $oldData['gname']?>" style="width: 800px;"/>
						</td>
					</tr>
					<tr class="info">
						<td>库存</td>
						<td>
							<span><?php echo $oldData['stock']?></span>
						</td>
					</tr>
					<tr class="info">
						<td>单位</td>
						<td>
							<input type="text" name='unit' value="<?php echo $oldData['unit']?>"/>
						</td>
					</tr>
					<tr class="info">
						<td>市价场</td>
						<td>
							<input type="text" name='marketprice' value="<?php echo $oldData['marketprice']?>"/>
						</td>
					</tr>
					<tr class="info">
						<td>商城价</td>
						<td>
							<input type="text" name='shopprice' value="<?php echo $oldData['shopprice']?>"/>
						</td>
					</tr>
					<tr class="info">
						<td>点击次数</td>
						<td>
							<input type="text" name='click' value="<?php echo $oldData['click']?>"/>
						</td>
					</tr>
				</tbody>
			</table>

			<p class="btn btn-primary">商品属性</p>
			<table class="table table-bordered table-hover" id='attr' class="info">
				<?php foreach ($data as $v){?>
					<?php if($v['class']=='属性' ){?>
                
						<tr class="info">	
							<td><?php echo $v['taname']?></td>
							<td>
								<select name="attr[<?php echo $v['taid']?>]">
									<option value="">-请选择-</option>
									<?php foreach ($v['tavalue'] as $value){?>
										<option value="<?php echo $value?>" <?php if($v['gavalue']==$value){?>
                selected
               <?php }?>><?php echo $value?></option>
									<?php }?>
								</select>
							</td>
						</tr>
					
               <?php }?>
				<?php }?>
			</table>
			<p class="btn btn-primary">商品规格</p>
			<table class="table table-bordered table-hover" id='spec'>
				<?php $taid = [];  ?>
				<?php foreach ($data as $v){?>
					<?php if($v['class']=='规格' ){?>
                
						<?php 
							$taid[]=$v['taid'];
						?>
						<tr class="info">
							<td><?php echo $v['taname']?></td>
							<td>
								<select name="spec[value][<?php echo $v['taid']?>][]" id="">
									<option value="">-请选择-</option>	
										<?php foreach ($v['tavalue'] as $value){?>
											<option value="<?php echo $value?>" <?php if($v['gavalue']==$value){?>
                selected
               <?php }?> ><?php echo $value?></option>
										<?php }?>
								</select>
							</td>
							<td>附加价格：<input type="text" name="spec[extraprice][<?php echo $v['taid']?>][]" value="<?php echo $v['extraprice']?>"/></td>
							<td><a href="javascript:;" class="btn btn-info removeSpec goodsAttr<?php echo $v['taid']?>">删除规格</a></td>
						</tr>
					
               <?php }?>
				<?php }?>
			</table>
			<?php  
			//去除重复的taid
				$taid = array_unique($taid);
			?>
			<script>
				var taid = '<?php echo json_encode($taid) ?>';
				//把js字符串转为json对象
				taid = $.parseJSON(taid);
				//循环json
				$.each(taid, function(k,v) {
					$('.goodsAttr' + v).first().removeClass('removeSpec btn-info').addClass('addSpec btn-primary').html('添加规格');
					
				})
			</script>
			<table class='table table-bordered'>
				<tr>
					<th colspan="3" class="btn btn-primary">列表图</th>
				</tr>
				<tr class="info">
					<td>上传图片</td>
					<td>
						<input id="listImg" type="file" multiple="true" >
						<!--显示要编辑的商品的旧列表图路径-->
						<img src="<?php echo $oldData['listview']?>" style="height: 100px;"/>
						<!--隐藏域，把旧列表图的路径post过去-->
						<input type="hidden" name="listview" value="<?php echo $oldData['listview']?>" />
    					<div id="uploadList">
				        	<ul>
			        		</ul>
				    	</div>
					</td>
				</tr>
			</table>

			<table class='table table-bordered'>
				<tr>
					<th colspan="3" class="btn btn-primary">商品图册</th>
				</tr>
				<tr class="info">
					<td>上传图片</td>
					<td>
						<input id="shopImg" type="file" multiple="true">
							<!--显示商品图册的小中大3张图-->
						<?php foreach ($oldData['gdpic'] as $v){?>
						<img src="<?php echo $v?>"/>
						<input type="hidden" name="gdpic[]" value="<?php echo $v?>"/>
						<?php }?>
    					<div id="uploadShopImg">
				        	<ul>
			        		</ul>
				    	</div>
					</td>
				</tr>
			</table>

			<table class='table'>
				<tr class="next_show">
					<th class="btn btn-primary">商品详情</th>
				</tr>
				<tr>
					<td>
						<!--调用百度编辑器-->
						<script id="editor" type="text/plain" style="width:100%;height:500px;" name="details"><?php echo $oldData['details']?></script>
			   			<script>
		        		var ue = UE.getEditor('editor');
		   				</script>
					</td>
				</tr>
			</table>
			<input type="hidden" name="stock" value="" />
			<!--要编辑的商品的gid的隐藏域-->
			<input type="hidden" name="gid"  value="<?php echo $oldData['gid']?>" />
			<!--隐藏域，把管理员账号传过去-->
			<input type="hidden" name="wsshop_admin_aid" value="<?php echo $_SESSION['info']['aid']?>" />
			<!--隐藏域，把类型ID传过去-->
			<input type="hidden" name='wsshop_type_tid'  value=""/>
			<input type="submit" value="确认编辑" class="btn btn-primary btn-block btn-large" />
		</fieldset>
	</form>

	</div>
	</div>
	</div>
</body>
</html>